package com.party.mobile.biz.subject;

import com.google.common.collect.Lists;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.PartyCode;
import com.party.core.exception.BusinessException;
import com.party.core.model.subject.Subject;
import com.party.core.model.subject.SubjectApply;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.subject.ISubjectApplyService;
import com.party.core.service.subject.ISubjectService;
import com.party.mobile.web.dto.subject.output.SubjectApplyOutput;
import com.party.mobile.web.dto.subject.output.SubjectOutput;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

@Service
public class SubjectBizService {

	@Autowired
	private ISubjectService subjectService;

	@Autowired
	private ISubjectApplyService subjectApplyService;

	@Autowired
	private IProjectService projectService;
	@Autowired
	private IMemberGroupService memberGroupService;

	/**
	 * 获取专题详情
	 *
	 * @param id
	 * @param request
	 * @return
	 */
	public SubjectOutput getDetails(String id, HttpServletRequest request) {

		Subject subject = subjectService.get(id);
		if (  null == subject || subject.getDelFlag().equals("1")) {
			throw new BusinessException(PartyCode.IS_NO_DATA, "动态的主键id错误，数据不存在.或主题不存在");
		}

		SubjectOutput output = SubjectOutput.transform(subject);

		List<SubjectApplyOutput> applyOutputs = Lists.newArrayList();
		List<SubjectApply> applies = subjectApplyService.getBySubjectId(id);
		for (SubjectApply entity : applies) {
			SubjectApplyOutput applyOutput = SubjectApplyOutput.transform(entity);
			applyOutputs.add(applyOutput);
		}

		output.setSubjectApplies(applyOutputs);
		return output;
	}

	/**
	 * 列表
	 *
	 * @param subject
	 * @param page
	 * @return
	 */
	public List<SubjectOutput> getList(Subject subject, Page page) {

		List<String> orgIds = memberGroupService.getChildWithMy(subject.getMemberId());
		subject.setOrgIds(orgIds);
		subject.setMemberId(null);

		List<Subject> subjects = subjectService.listPage(subject, page);

		if (!CollectionUtils.isEmpty(subjects)) {
			List<SubjectOutput> subjectOutputs = LangUtils.transform(subjects, input -> {
				SubjectOutput output = SubjectOutput.transform(input);
				return output;
			});
			return subjectOutputs;
		}
		return Collections.EMPTY_LIST;
	}

    /**
     * 获取我参加的众筹的机构的手机官网
     *
     * @param currentId 当前用户
     * @param page      分页
     * @return 手机官网集合
     */
    public List<SubjectOutput> getMySubject(String currentId, Page page) {
        List<String> partnerIds = projectService.getPartnerIdByAuthorId(currentId);
        if (partnerIds.size() == 0) {
            return Collections.EMPTY_LIST;
        }
        List<Subject> subjects = subjectService.batchByMember(new HashSet<>(partnerIds), page);
        if (!CollectionUtils.isEmpty(subjects)) {
            List<SubjectOutput> subjectOutputs = LangUtils.transform(subjects, input -> {
                SubjectOutput output = SubjectOutput.transform(input);
                return output;
            });
            return subjectOutputs;
        }
        return Collections.EMPTY_LIST;
    }
}
